package priv.kz.client;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FileDialog;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

/**
 * 窗体文件
 * @author Administrator
 *
 */
public class MainFrame extends JFrame {

	/**
	 * 表格的数据模型
	 * @author Administrator
	 *
	 */
	public class MyTableModel extends DefaultTableModel {
		List<Map> list=new ArrayList<>();
		
		Class[] columnTypes = new Class[] { String.class, String.class,
				String.class, Boolean.class };//表格列类型的数组

		public MyTableModel(Object[][] dataArray) {//获取表格的标题
			setDataVector(dataArray, new String[] { "\u6587\u4EF6\u540D",
					"\u4FEE\u6539\u65F6\u95F4", "\u6587\u4EF6\u7C7B\u578B",
					"\u9009\u62E9" });
		}

		

		public Class getColumnClass(int columnIndex) {//获取表格的列的类型
			return columnTypes[columnIndex];//返回类型数组中对应的值
		}

		@Override
		public int getColumnCount() {//获取表格的列的数量
			return columnTypes.length;
		}

	}

	private JPanel contentPane;
	private JTable table;
	private Object[][] dataArray;
	private MyTableModel dataModel;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {//启动窗体
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainFrame frame = new MainFrame();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public MainFrame() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 510, 385);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);

		JLayeredPane layeredPane = new JLayeredPane();
		contentPane.add(layeredPane, BorderLayout.CENTER);

		JButton button = new JButton("\u4E0A\u4F20\u6587\u4EF6");
		button.addActionListener(new ActionListener() {//上传按钮被点击
			public void actionPerformed(ActionEvent e) {
				FileDialog fileDialog = new FileDialog(MainFrame.this, "ѡϴļ",
						FileDialog.LOAD);
				fileDialog.setVisible(true);
				String file = fileDialog.getFile();//获取用户选中的文件名
				String directory = fileDialog.getDirectory();//获取用户选中的目录
				FileClient.uploadFile(directory + file);//进行文件上传业务
			}
		});
		button.setBounds(356, 313, 126, 25);
		layeredPane.add(button);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(10, 10, 472, 281);
		layeredPane.add(scrollPane);

		table = new JTable();//初始化表格
		try {
			dataArray = FileClient.getFiles();//从服务器获取文件列表
		} catch (ClassNotFoundException | IOException e1) {
			e1.printStackTrace();
		}
		 dataModel = new MyTableModel( dataArray);//初始化表格数据模型
		
		table.setModel(dataModel);
		scrollPane.setViewportView(table);

		JButton button_1 = new JButton("下载文件");
		button_1.addActionListener(new ActionListener() {//下载按钮被点击
			public void actionPerformed(ActionEvent e) {
//				====================获取用户选择的目录(用来存放下载的文件)=========================
				String directory = null;
				JFileChooser jFileChooser = new JFileChooser(".");
				jFileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
				int res = jFileChooser.showSaveDialog(null);
				if(res==jFileChooser.APPROVE_OPTION){
					directory=jFileChooser.getSelectedFile().getAbsolutePath();
				}
//				====================过滤用户选中的要下载的文件=========================
				List<String> list=new ArrayList<>();
				for (Object string : dataModel.getDataVector()) {
					Vector vec=(Vector) string;
					if((boolean) vec.get(3)){//判断是否被选中
						list.add((String) vec.get(0));//如果被选中则添加
					}
				}
				try {
					FileClient.downLoad(list,directory);//向服务器请求文件下载
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		});
		button_1.setBounds(222, 313, 126, 25);
		layeredPane.add(button_1);
	}
}
